<?php
if(!defined('IN_IBEIKE')) {
	exit('Access Denied');
}

class table_common_tag extends common_table
{
	public function __construct() {

		$this->_table = 'common_tag';
		$this->_pk    = 'tagid';

		parent::__construct();
	}
	public function fetch_all($start=0,$limit=0,$sort='DESC'){
		return DB::fetch_all('SELECT * FROM %t ORDER BY tagcount DESC '.DB::limit($start,$limit),array($this->_table));
	}
	public function fetch_all_by_tagid($tagid){
		if(!empty($tagid)&&$tagid){
			return DB::fetch_all('SELECT * FROM %t WHERE tagid=%s',array($this->_table,$tagid));//返回数组包含各个字段的值
		}
		return false;
	}
	public function update_by_tagid($tagid,$data,$unbuffered = false, $low_priority = false){
		if(!empty($tagid) && !empty($data)){
			$condition=$this->_pk."='".$tagid."'";
			return DB::update($this->_table,$data,$condition,$unbuffered,$low_priority);//返回前一次 MySQL 操作所影响的记录行数
		}
		return false;
	}
	public function insert($data, $return_insert_id = false, $replace = false, $silent = false){//只能对一条数据进行操作
		if(!empty($data)){
			return DB::insert($this->_table,$data, $return_insert_id,$replace,$silent);//返回上一步 INSERT 操作产生的 ID
		}
		return false;
	
	}
	public function insert_tag_by_tagname($tagid,$tagname,$taghash){//通过数组来写入记录
		if(!empty($tagid) && !empty($tagname) && !empty($taghash)){
			
			$num=self::fetch_by_tag_name($tagname);
			if($num==0){
				$arg=array();
			    $arg['tagid']=$tagid;
			    $arg['taghash']=$taghash;
			    $arg['tagname']=$tagname;
				return DB::insert($this->_table,$arg);
			}else{
				$arg=array();
				$arg['tagcount']=$num['tagcount']+1;
				$condition='tagname='.$tagname.' and tagid='.$tagid;
				return DB::update($this_table,$arg,$condition);
			}
		}
		return false;
	}
	public function fetch_by_tag_name($tagname){
		if(!empty($tagname)){
			 $line=DB::fetch_first("seclet tagname from %t where tagname=%s",array($this->_table,$tagname));
			 //$line=DB::num_rows($resourceid);
			 if(empty($line)){
			 	return 0;
			 }else{
			 	return $line;
			 }
		}else{
			return false;
		}
	}
	public function get_hot_tag($limit=30){
		return DB::fetch_all("SELECT * FROM %t LIMIT %d ORDER BY tagcount",array($this->_table,$limit));//获取前30条热点信息,返回的为数组
	}
	public function fecth_like_tagname($tagname,$limit){
		if(empty($tagname)){
			return DB::fecth_all("SECLET tagname FROM %t LIMIT %d",array($this->_table,$limit));
		}else{
			return DB::fecth_all("SECLET tagname FROM %t WHERE tagname LIKE %s LIMIT %d",array($this->_table,$tagname."%",$limit));
		}
	}
}
?>